﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraMove : MonoBehaviour
{
    private Transform camTrans;

    [SerializeField]
    private Vector3 camHeight;
    [SerializeField]
    private float speed = 1f;
    [SerializeField] 
    private float rotateSpeed = 1.5f;
    [SerializeField]
    private float jumpHeight = 5f;

    [SerializeField] private float jumpRate = 1f;

    private bool canJump = true;
    private CharacterController characterController;

    // Start is called before the first frame update
    void Start()
    {
        camTrans = Camera.main.transform;
        Vector3 startPos = transform.position;
        startPos += camHeight;
        camTrans.position = startPos;

        camTrans.rotation = transform.rotation;
        characterController = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        float rotateY = Input.GetAxis("Mouse X");
        float rotateX = Input.GetAxis("Mouse Y");
        camTrans.eulerAngles += new Vector3(-rotateX, rotateY, 0) * rotateSpeed;
        camTrans.position = new Vector3(transform.position.x, camTrans.position.y, transform.position.z);
        transform.eulerAngles = new Vector3(transform.eulerAngles.x, camTrans.eulerAngles.y, transform.eulerAngles.z);

        float vertical = Input.GetAxis("Vertical");
        float horizontal = Input.GetAxis("Horizontal");

        characterController.SimpleMove(transform.forward * vertical * speed);
        characterController.SimpleMove(transform.right * horizontal * speed);

        if (!canJump && characterController.isGrounded)
        {
            canJump = true;
        }

        if (Input.GetKeyDown((KeyCode.Space)) && canJump)
        {
            canJump = false;
            transform.position =
                Vector3.Lerp(transform.position, transform.position + new Vector3(0, jumpHeight, 0), 0.7f);
        }
        Vector3 startPos = transform.position;
        startPos += camHeight;
        camTrans.position = startPos;
    }

    private void OnControllerColliderHit(ControllerColliderHit hit)
    {
        //Debug.Log(hit.collider.gameObject);
    }
}
